package com.snail.billing;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import com.android.net.HttpApp;
import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.snail.billing.data.Account;
import com.snail.billing.data.AccountManager;
import com.snail.billing.page.DialogChangePage;
import com.snail.billing.page.DialogUpdatePage;
import com.snail.billing.page.LaunchPage;
import com.snail.billing.page.LoginPage;
import com.snail.billing.page.view.FloatView;
import com.snail.billing.session.CreateImprestOrderSession;
import com.snail.billing.session.CreateOrderSession;

public class BillingLogic {

	// private static final String TAG = BillingLogic.class.getName();

	private Context context;
	private HttpApp httpApp;
	private static FloatView floatView;

	public BillingLogic(Context context) {
		this.context = context;
	}

	private Context getContext() {
		return context;
	}

	public void launch() {
		Account account = AccountManager.getCurrentAccount();
		if (account == null) {
			if (AccountManager.getAccountList().length() == 0) {
				DataCache.getInstance().importParams.pageClass = LaunchPage.class;
				getContext().startActivity(new Intent(getContext(), Billing.class));
			} else {
				DataCache.getInstance().importParams.pageClass = LoginPage.class;
				getContext().startActivity(new Intent(getContext(), Billing.class));
			}
		} else {
			if (account.getType().equals(Account.TYPE_RANDOM) || account.getType().equals(Account.TYPE_RANDOM_OLD)) {
				DataCache.getInstance().importParams.pageClass = DialogUpdatePage.class;
				getContext().startActivity(new Intent(getContext(), Billing.class));
			} else {
				DataCache.getInstance().importParams.pageClass = DialogChangePage.class;
				Intent intent = new Intent(getContext(), Billing.class);
				intent.putExtra(DialogChangePage.ACCOUNT, account.getAccount());
				intent.putExtra(DialogChangePage.PWD, account.getPwd());
				intent.putExtra(DialogChangePage.TYPE, account.getType());
				getContext().startActivity(intent);
			}
		}
	}

	public void createOrderNo() {
		httpApp = new HttpApp(getContext());
		//
		if (DataCache.getInstance().importParams.partnerId.equals("007")) {
			final CreateOrderSession createOrderSession = new CreateOrderSession();
			httpApp.setOnHttpResultListener(new OnHttpResultListener() {

				@Override
				public void onHttpResult(HttpResult httpResult) {
					if (httpResult.isSucceed()) {
						HttpSession httpSession = httpResult.getHttpSession();
						CreateOrderSession.JsonData jsonData = new CreateOrderSession.JsonData((String) httpSession.getResponseData());
						if (jsonData.getCode() == 1) {
							String orderNo = jsonData.getOrderNo();
							String action = BillingCallback.ACTION_CREATE_ORDER_NO;
							DataCache.getInstance().importParams.billingCallback.onCallback(1, action, new String[] { orderNo });
						} else {
							Toast.makeText(getContext(), jsonData.getMessage(), Toast.LENGTH_SHORT).show();
						}
					}
				}
			});
			httpApp.request(createOrderSession);
		} else {
			final CreateImprestOrderSession createImprestOrderSession = new CreateImprestOrderSession();
			httpApp.setOnHttpResultListener(new OnHttpResultListener() {

				@Override
				public void onHttpResult(HttpResult httpResult) {
					if (httpResult.isSucceed()) {
						HttpSession httpSession = httpResult.getHttpSession();
						CreateImprestOrderSession.JsonData jsonData = new CreateImprestOrderSession.JsonData((String) httpSession.getResponseData());
						if (jsonData.getCode() == 1) {
							String orderNo = jsonData.getOrderNo();
							String action = BillingCallback.ACTION_CREATE_ORDER_NO;
							DataCache.getInstance().importParams.billingCallback.onCallback(1, action, new String[] { orderNo });
						} else {
							Toast.makeText(getContext(), jsonData.getMessage(), Toast.LENGTH_SHORT).show();
						}
					}
				}
			});
			httpApp.request(createImprestOrderSession);
		}
	}

	public void showUserCenterFloatView() {
		floatView = new FloatView(getContext());
		floatView.setVisibility(View.VISIBLE);
	}

	public void dismissUserCenterFloatView() {
		if (floatView != null) {
			floatView.setVisibility(View.GONE);
			floatView.destory();
			floatView = null;
		}
	}

}
